package com.ysstech.reportworld.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.DataDemandBug;
import com.ysstech.reportworld.entity.DemandBugInfo;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.service.DataDemandBugService;
import com.ysstech.reportworld.service.DemandBugInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 报表世界需求bug信息表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-12-27
 */
@Controller
@Slf4j
@Api(tags = "报表世界需求BUG管理api")
@RequestMapping("/demandBugInfo")
public class DemandBugInfoController extends BaseController {
    @Autowired
    private DemandBugInfoService demandBugInfoService;
    @Autowired
    private DataDemandBugService dataDemandBugService;

    /**
     * 跳转需求BUG管理信息页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "跳转需求BUG管理页面")
    @YssLog(module = "需求BUG管理", remark = "跳转需求BUG管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "reportworld/demandBugInfo";
    }


    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "获取需求BUG管理页面初始值")
    @YssLog(module = "需求BUG管理", remark = "获取需求BUG管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 状态
        List<Dicvalue> listStatus = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_STATUS.getCode());
        map.put("listStatus", listStatus);
        map.put("listStatus2", listStatus);
        // 版本
        List<Dicvalue> listVersion = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_VERSION.getCode());
        map.put("listVersion", listVersion);
        map.put("listVersion2", listVersion);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 需求BUG列表查询
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "获取所有需求BUG信息")
    @YssLog(module = "需求BUG管理", remark = "获取所有需求BUG信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(DemandBugInfo demandBugInfo, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取所有需求BUG信息成功",
                demandBugInfoService.queryAllDemandBugInfo(demandBugInfo, this.getPage(page, rows)));
    }

    /**
     * 新增需求BUG的方法
     *
     * @param demandBugInfo
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo:add"})
    @ApiOperation(value = "新增需求BUG信息")
    @YssLog(module = "需求BUG管理", remark = "新增需求BUG信息")
    @RequestMapping(value = "/addDemandBugInfo")
    @ResponseBody
    public RestResult addDemandBugInfo(DemandBugInfo demandBugInfo) throws Exception {
        if (null == demandBugInfo) {
            throw new BusinessException("获取新增报表世界需求BUG信息失败，请稍后重试！");
        }
        demandBugInfo.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        demandBugInfo.setAddTime(LocalDateTime.now());
        demandBugInfo.setId(DateUtil.generateUUID());
        demandBugInfoService.save(demandBugInfo);
        return ResultGenerator.getSuccessResult("新增需求BUG信息成功！");
    }

    /**
     * 修改需求BUG信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo:edit"})
    @ApiOperation(value = "修改需求BUG信息")
    @YssLog(module = "需求BUG管理", remark = "修改需求BUG信息")
    @RequestMapping(value = "/editDemandBugInfo")
    @ResponseBody
    public RestResult editDemandBugInfo(DemandBugInfo demandBugInfo) throws Exception {
        if (demandBugInfo == null || StringUtils.isBlank(demandBugInfo.getId())) {
            throw new BusinessException("获取需求BUG信息失败，请稍后重试！");
        }
        demandBugInfo.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        demandBugInfo.setEditTime(LocalDateTime.now());
        demandBugInfoService.updateById(demandBugInfo);
        return ResultGenerator.getSuccessResult("修改需求BUG信息成功！");
    }

    /**
     * 删除需求BUG信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo:del"})
    @ApiOperation(value = "删除需求BUG信息")
    @YssLog(module = "需求BUG管理", remark = "删除需求BUG信息")
    @RequestMapping(value = "/delDemandBugInfo")
    @ResponseBody
    @Transactional
    public RestResult delDemandBugInfo(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除报表世界需求BUG信息失败，请稍后重试！");
        }
        List<String> idList = JSONArray.parseArray(ids, String.class);
        if (!CollectionUtils.isEmpty(idList)) {
            demandBugInfoService.removeByIds(idList);
            // 删除数据和需求BUG对应关系
            QueryWrapper<DataDemandBug> wrapper = new QueryWrapper<>();
            wrapper.in("DATA_ID", idList);
            dataDemandBugService.remove(wrapper);
        }
        return ResultGenerator.getSuccessResult("删除需求BUG信息成功！");
    }


    /**
     * 获取需求BUG信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "获取需求BUG信息")
    @YssLog(module = "需求BUG管理", remark = "获取需求BUG信息")
    @PostMapping(value = "/queryDemandBugInfoById")
    @ResponseBody
    public RestResult queryDemandBugInfoById(DemandBugInfo demandBugInfo) throws Exception {
        if (null == demandBugInfo || StringUtils.isBlank(demandBugInfo.getId())) {
            throw new BusinessException("获取对应的需求BUG信息失败，请稍后重试！");
        }
        QueryWrapper<DemandBugInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("ID", demandBugInfo.getId());
        return ResultGenerator.getSuccessResult("获取需求BUG信息成功",
                demandBugInfoService.getOne(wrapper));
    }

    /**
     * 获取数据对应的需求BUG信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo"})
    @ApiOperation(value = "获取数据对应的需求BUG信息")
    @YssLog(module = "需求BUG管理", remark = "获取数据对应的需求BUG信息")
    @RequestMapping(value = "/selectByPrimaryByDataId")
    @ResponseBody
    public RestResult selectByPrimaryByDataId(String dataId, String dataCode) throws Exception {
        if (StringUtils.isBlank(dataId) || StringUtils.isBlank(dataCode)) {
            throw new BusinessException("获取数据对应的需求BUG信息失败，请稍后重试！");
        }
        List<DemandBugInfo> list = demandBugInfoService.selectByPrimaryByDataId(dataId, dataCode);
        return ResultGenerator.getSuccessResult("获取数据对应的需求BUG信息成功！", list);
    }


    /**
     * 判断需求BUG编号是不是已经存在
     *
     * @param demandBugNum
     * @return
     */
    @ApiOperation(value = "检查需求BUG编号是否存在")
    @YssLog(module = "需求BUG管理", remark = "检查需求BUG编号是否存在")
    @RequestMapping(value = "/checkDemandBugInfo")
    @ResponseBody
    public RestResult checkDemandBugInfo(String demandBugNum, Integer demandBugType) {
        if (StringUtils.isBlank(demandBugNum) || null == demandBugType) {
            return ResultGenerator.getFailResult("检查需求BUG编号信息失败，请稍后重试！");
        }
        QueryWrapper<DemandBugInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("DEMAND_BUG_NUM", demandBugNum);
        wrapper.eq("DEMAND_BUG_TYPE", demandBugType);
        int count = demandBugInfoService.count(wrapper);
        //查询是否存在code值得参数
        if (count > 0) {//返回true表示已经存在
            return ResultGenerator.getSuccessResult("检查需求BUG编号信息成功", false);
        } else {
            return ResultGenerator.getSuccessResult("检查需求BUG编号信息成功", true);
        }
    }


    /**
     * 从项目管理计划文件中读取当前版本的需求
     *
     * @param multipartfile
     * @return
     */
    @YssPermissions(value = {"reportworld:demandbuginfo:readFile"})
    @ApiOperation(value = "读取项目管理计划当前版本的需求")
    @YssLog(module = "需求BUG管理", remark = "读取项目管理计划当前版本的需求")
    @RequestMapping(value = "/readDemandBugInfo")
    @ResponseBody
    public RestResult readDemandBugInfo(MultipartFile multipartfile) throws Exception {
        Map<String, String> map = new HashMap<>();
        String msgInfo = demandBugInfoService.readDemandBugInfo(multipartfile);
        map.put("msgInfo", msgInfo);
        return ResultGenerator.getSuccessResult("读取项目管理计划当前版本的需求成功", map);
    }


}
